package com.hoshiicloud.mallshop.service.impl.goodsmanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.param.ListSkuSpuParam;
import com.hoshiicloud.goods.param.sku.BranchStoreProductPageParam;
import com.hoshiicloud.goods.vo.BranchStoreGoodsVo;
import com.hoshiicloud.goods.vo.ListSpuSkuVo;
import com.hoshiicloud.mallshop.feign.ShopGoodsSpuSkuFeignService;
import com.hoshiicloud.mallshop.feign.ShopUserFeignService;
import com.hoshiicloud.mallshop.service.goodsmanage.BranchStoreProductService;
import com.hoshiicloud.user.entity.store.PlatStore;
import com.hoshiicloud.user.param.PlatStoreQueryParam;
import com.hoshiicloud.user.vo.store.PlatStoreBranchVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  2019/9/10 14:36 qry
 */
@Slf4j
@Service
public class BranchStoreProductServiceImpl implements BranchStoreProductService {
    private final ShopUserFeignService shopUserFeignService;
    private final ShopGoodsSpuSkuFeignService shopGoodsFeignService;
    public BranchStoreProductServiceImpl(ShopUserFeignService shopUserFeignService, ShopGoodsSpuSkuFeignService shopGoodsFeignService) {
        this.shopUserFeignService = shopUserFeignService;
        this.shopGoodsFeignService = shopGoodsFeignService;
    }


    @Override
    @Logable(authName = "店铺聚合(店铺后台)-商品管理-分店商品-获取分店分页列表")
    public Page<PlatStoreBranchVo> getBranchStoresPage(PlatStoreQueryParam qp) {
        //获取分店分页
        Page<PlatStore> pagePlatStore= this.getPlatStorePage(qp);
        List<PlatStore>list1 = pagePlatStore.getRecords();
        Page<PlatStoreBranchVo> rs= new Page<>(Long.parseLong(qp.getPageNumber()), Long.parseLong(qp.getPageSize()));
        if (CollectionUtils.isEmpty(list1)) {
            return rs;
        }
        List<Long> listStoreIds = list1.stream().map(PlatStore::getId).collect(Collectors.toList());
        // 查询该页  分店 各自的 数量 并赋值
        List<PlatStoreBranchVo> list2 = getPlatStoreBranchSkuNums(list1, listStoreIds);
        rs.setRecords(list2);
        rs.setTotal(pagePlatStore.getTotal());
        rs.setPages(pagePlatStore.getPages());
        return rs;
    }

    @Override
    @Logable(authName = "店铺聚合(店铺后台)-商品管理 -分店商品-获取某分店商品分页列表")
    public Response<Page<BranchStoreGoodsVo>> listBranchStoreGoods(BranchStoreProductPageParam param) {
        Response<Page<BranchStoreGoodsVo>>  rs =shopGoodsFeignService.listBranchStoreGoods(param);
        if(null == rs || rs.getCode() !=200 || rs.getData() == null){
            throw new ServiceException("获取分店商品分页信息失败");
        }
        return rs;
    }

    @Override
    @Logable(authName = "店铺聚合(店铺后台)-商品管理 -分店商品-添加商品-可添加SKU分页列表")
    public Response<Page<ListSpuSkuVo>> getBranchStoreCanAddSkuPage(ListSkuSpuParam param) {
        Response<Page<ListSpuSkuVo>> rs = shopGoodsFeignService.getBranchStoreCanAddSkuPage(param);
        if(null == rs || rs.getCode() !=200 || rs.getData() == null){
            throw new ServiceException("获取可添加SKU分页列表失败");
        }
        return rs;
    }


    private List<PlatStoreBranchVo> getPlatStoreBranchSkuNums(List<PlatStore> list1, List<Long> listStoreIds) {
        Map<String, String> map = getBranchStoreSkuNums(listStoreIds);
        List<PlatStoreBranchVo> list2 = new ArrayList<>();
        for (PlatStore p :list1 ){
            PlatStoreBranchVo platStoreBranchVo;
            try {
                platStoreBranchVo = BeanUtils.covertType(p, PlatStoreBranchVo.class);
            } catch (Exception e) {
                log.error("Bean转换异常",e);
                throw new ServiceException("Bean转换异常");
            }
            platStoreBranchVo.setDistributorCn(platStoreBranchVo.getIsDistributor() == 1?"是分销商":"非分销商");
            platStoreBranchVo.setSkuNum(map.getOrDefault(platStoreBranchVo.getId().toString(),"0"));
            list2.add(platStoreBranchVo);
        }
        return list2;
    }

    private Map<String, String> getBranchStoreSkuNums(List<Long> listStoreIds) {
        Response<Map<String,String>> rs = shopGoodsFeignService.getStoreSkuNum(listStoreIds);
        if(null == rs || rs.getCode() !=200 || rs.getData() == null){
            throw new ServiceException("获取分店对应的sku数量失败");
        }
        return rs.getData();
    }

    private Page<PlatStore> getPlatStorePage(PlatStoreQueryParam qp) {
        Response<Page<PlatStore>> rs= shopUserFeignService.getPlatStorePage(qp);
        if(null == rs || rs.getCode() !=200 || rs.getData() == null){
            throw new ServiceException("获取分店信息失败");
        }
        return rs.getData();
    }
}
